package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.base.BaseOrder;
import com.onlineMIS.ORM.entity.headQ.finance.ExpenseHqItem;
import com.onlineMIS.ORM.entity.headQ.finance.ExpenseHqType;
import com.onlineMIS.common.Common_util;

@Repository
public class ExpenseHqItemImpl extends BaseDAO<ExpenseHqItem>{
	
	@Autowired
	private ExpenseHqTypeImpl expenseHqTypeImpl;

	
	/**
	 * 根据总部费用单据id查询单据费用支出详情
	 * @param id
	 * @return
	 */
	public List<ExpenseHqItem> selectItemsByExpenseId(int id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqItem.class);
		criteria.add(Restrictions.eq("expenseHq.id", id));
		List<ExpenseHqItem> items =  getByCritera(criteria, true);
		if (items != null ) {
			for (ExpenseHqItem expenseHqItem : items) {
				ExpenseHqType type = expenseHqTypeImpl.get(expenseHqItem.getExpenseHqType().getParentId(), true);
				if (type != null) {
					expenseHqItem.getExpenseHqType().setParentName(type.getName());
				}
				 
			}
		}
       return items;
	}

	/**
	 * 根据总部费用ID删除费用详情信息
	 * @param id
	 */
	public void deleteByExpenseHqId(int id) {
		String deleteString = "DELETE FROM ExpenseHqItem WHERE expenseHq.id = " + id;
		
	    this.executeHQLUpdateDelete(deleteString, null, true);
	}

	
	/**
	 * 根据时间查询总部费用详情
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<ExpenseHqItem> selectExpenseHqItems(Date startDate, Date endDate,int type,int financeCategoryId,int expenseHqId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHqItem.class);
		DetachedCriteria hqCriteria = criteria.createCriteria("expenseHq");
		if (expenseHqId != Common_util.ALL_RECORD_NEW) {
			hqCriteria.add(Restrictions.eq("id",expenseHqId));
		}else {
			hqCriteria.add(Restrictions.between("expenseDate", Common_util.formStartDate(startDate), Common_util.formEndDate(endDate)));
			hqCriteria.add(Restrictions.eq("status",BaseOrder.STATUS_COMPLETE));
			if (type != Common_util.ALL_RECORD) {
				hqCriteria.add(Restrictions.eq("type",type));
			}
			if (financeCategoryId != Common_util.ALL_RECORD) {
				hqCriteria.add(Restrictions.eq("financeCategory.id",financeCategoryId));
			}
		}
		
		List<ExpenseHqItem> items =  getByCritera(criteria, true);
		if (items != null ) {
			for (ExpenseHqItem expenseHqItem : items) {
				ExpenseHqType item = expenseHqTypeImpl.get(expenseHqItem.getExpenseHqType().getParentId(), true);
				if (item != null) {
					expenseHqItem.getExpenseHqType().setParentName(item.getName());
				}
				 
			}
		}
       return items;
	}




	
}
